package java44th.group.controller.admin.ad;

import java44th.group.bean.admin.bo.ad.AdCreateBo;
import java44th.group.bean.admin.bo.ad.AdListBo;
import java44th.group.bean.po.MarketAd;
import java44th.group.bean.admin.vo.BaseRespVo;
import java44th.group.bean.admin.vo.ad.AdListVo;
import java44th.group.service.admin.ad.AdService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Aurhor: 帅的一批
 * @CreateAt: 2022/10/11 19:43
 * @Description:
 * @PACKAGE_NAME： java44th.group.controller.admin
 */

@RestController
@RequestMapping("admin/ad")
public class AdController {

    @Autowired
    AdService adService;

    @RequiresPermissions(value={"admin:ad:list","*"},logical= Logical.OR)
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public BaseRespVo getAdList(AdListBo adListBo) {
        AdListVo adListVo = adService.queryAdListOrderAndLimit(adListBo);
        if (adListBo == null) {
            return BaseRespVo.invalidData("别搞啦");
        }
        return BaseRespVo.ok(adListVo);
    }

    //    @RequestMapping(value = "read",method = RequestMethod.GET)
//    public BaseRespVo read(){
//
//    }
//
    @RequiresPermissions("admin:ad:create")
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public BaseRespVo createAd(@RequestBody AdCreateBo adCreateBo) {
        MarketAd ad = adService.createAd(adCreateBo);
        if (ad == null) {
            return BaseRespVo.invalidData("新增广告失败");
        }
        return BaseRespVo.ok(ad);
    }

    //
    @RequiresPermissions("admin:ad:delete")
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public BaseRespVo deleteAd(@RequestBody AdCreateBo adCreateBo) {
        try {
            adService.deleteAd(adCreateBo);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidData("别搞!");
        }

        return BaseRespVo.ok(null);
    }

    //
    @RequiresPermissions("admin:ad:update")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public BaseRespVo updateAd(@RequestBody AdCreateBo adCreateBo) {
        MarketAd marketAd = adService.updateAd(adCreateBo);
        if (marketAd == null) {
            return BaseRespVo.invalidData("数据更新失败 检查数据是否正常");
        }
        return BaseRespVo.ok(marketAd);
    }

}
